#include "CShape.h"

CShape::CShape()
{
	m_numberVerts = 0;
	m_numTriangles = 0;
	m_pTextureCoordinates = NULL;
	m_pVertexData = NULL;
}

CShape::~CShape()
{
	if(m_pVertexData)
	{
		delete [] m_pVertexData;
	}
	if(m_pTextureCoordinates)
	{
		delete [] m_pTextureCoordinates;
	}
}

void CShape::MakeShape(Vec2 vecTopLeft, Vec2 vecBottomRight, int nDivisions)
{
	if(nDivisions < 1)
	{
		//must have 1 or more divisions
		return;
	}

	m_numberVerts = (nDivisions+1)*2;
	float vertDistance = (vecBottomRight.x - vecTopLeft.x);
	//these are the steps between the min/max positions
	float divisionStep = vertDistance / nDivisions;
	float heightStep = vecBottomRight.y - vecTopLeft.y;
	m_numTriangles = nDivisions * 2;

	m_pVertexData = new Vec2[m_numberVerts];
	m_pTextureCoordinates = new Vec2[m_numberVerts];

	for(unsigned int iter = 0; iter < m_numberVerts; iter++)
	{
		//Vertex buffer
		Vec2 writeVec(divisionStep * (iter/2), heightStep * (iter%2));
		writeVec += vecTopLeft;
		m_pVertexData[iter] = writeVec;
		
		//Texcoords
		writeVec = Vec2((divisionStep * (iter/2)) / vertDistance, iter%2);
		m_pTextureCoordinates[iter] = writeVec;
	}
}

void CShape::MakeNoodle(float lefttheta, float righttheta, float innerlimit, float outerlimit, int nDivisions)
{
	if ( nDivisions < 1 )
	{
		return ;
	}
	m_numberVerts = (nDivisions+1)*2;

	float thetaStep = (lefttheta - righttheta) / nDivisions;

	Vec3 vecNEAR(0.0f, innerlimit, 0.0f), vecFAR(0.0f, outerlimit, 0.0f);

	vecFAR = vecFAR * Mat4::GetRotateZ( -lefttheta );
	vecNEAR = vecNEAR * Mat4::GetRotateZ( -righttheta );

	MakeShape(Vec2(vecFAR.x, vecFAR.y), Vec2(vecNEAR.x, vecNEAR.y), nDivisions);

	for(int iter = 1; iter < m_numberVerts-1; iter++)
	{
		float moveTheta = lefttheta - thetaStep * (iter/2);
		Vec3 tempVec = Vec3(0, (iter & 1) ? innerlimit : outerlimit, 0) * Mat4::GetRotateZ(-moveTheta); 
		m_pVertexData[iter] = Vec2(tempVec.x, tempVec.y); 
	}
}